package com.snap.vseries.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.snap.vseries.R;
import com.snap.vseries.bean.ProcessBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.jessyan.autosize.utils.AutoSizeUtils;

public class RunningProcessAdapter extends RecyclerView.Adapter<RunningProcessAdapter.Holder> {

    private static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    private List<ProcessBean> processBeans;

    public RunningProcessAdapter(List<ProcessBean> processBeans) {
        this.processBeans = processBeans;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_process, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        ProcessBean bean = processBeans.get(position);
        holder.timeTv.setText(sdf.format(new Date(bean.getSec())));
        holder.contentTv.setText(bean.getSummary());
        if (bean.getCycleIndex() > 1 && bean.getActionIndex() == 1) {
            holder.contentTv.append(String.format("(第%d次）", bean.getCycleIndex()));
        }
        Context context = holder.itemView.getContext();
        holder.contentTv.setTextColor(position == getItemCount() - 1 ?
                context.getResources().getColor(R.color.colorPrimary) : context.getResources().getColor(R.color.txt_primary));
        holder.contentTv.setTextSize(AutoSizeUtils.sp2px(context, bean.getPriority() > 8 ? 18 : 16));
        holder.contentTv.getPaint().setFakeBoldText(bean.getPriority() > 8);
        holder.headTv.setVisibility(View.GONE);
        if (position > 0) {
            ProcessBean pre = processBeans.get(position - 1);
            if (bean.getStepIndex() > pre.getStepIndex()) {
                holder.headTv.setVisibility(View.VISIBLE);
                holder.headTv.setText("步骤" + bean.getStepIndex());
            }
        }
    }

    @Override
    public int getItemCount() {
        return processBeans == null ? 0 : processBeans.size();
    }

    static class Holder extends RecyclerView.ViewHolder {

        @BindView(R.id.time_tv)
        TextView timeTv;
        @BindView(R.id.content_tv)
        TextView contentTv;
        @BindView(R.id.head_tv)
        TextView headTv;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
